package com.dg.river.core.manager;

import android.content.Context;
import android.os.Build;

import com.dg.river.core.util.SPUtils;

/**
 * Created by hy on 2018/11/16
 * Description : 用户信息管理器
 */
public class UserInfoManager {

    private static UserInfoManager userInfoManager;

    private Context mContext;

    public static UserInfoManager getInstance() {
        if (userInfoManager == null) {
            synchronized (UserInfoManager.class) {
                if (userInfoManager == null) {
                    userInfoManager = new UserInfoManager();
                }
            }
        }
        return userInfoManager;
    }

    public void init(Context mContext) {
        this.mContext = mContext;
    }

    // 获取用户的手机型号id
    public String getPhoneBrand() {
        return Build.MODEL;
    }

    /**
     * 设备token
     *
     * @param userInfoMethod
     * @param tripToken
     * @param hasBearer      set时无用
     * @return
     */
    public String tripToken(UserInfoMethod userInfoMethod, String tripToken, boolean hasBearer) {
        if (userInfoMethod == UserInfoMethod.GET) {
            if (hasBearer) {
                return "Bearer " + SPUtils.getInstance(mContext).getString(UserInfoEnum.TRIP_TOKEN.VALUE, tripToken);
            } else {
                return SPUtils.getInstance(mContext).getString(UserInfoEnum.TRIP_TOKEN.VALUE, tripToken);
            }
        } else {
            SPUtils.SharedPreferencesCompat.apply(SPUtils.getInstanceEdit(mContext).putString(UserInfoEnum.TRIP_TOKEN.VALUE, tripToken));
        }
        return "";
    }

    /**
     * 用户token
     *
     * @param userInfoMethod
     * @param userToken
     * @param hasBearer      set时无用
     * @return
     */
    public String userToken(UserInfoMethod userInfoMethod, String userToken, boolean hasBearer) {
        if (userInfoMethod == UserInfoMethod.GET) {
            if (hasBearer) {
                return "Bearer " + SPUtils.getInstance(mContext).getString(UserInfoEnum.USER_TOKEN.VALUE, userToken);
            } else {
                return SPUtils.getInstance(mContext).getString(UserInfoEnum.USER_TOKEN.VALUE, userToken);
            }
        } else {
            SPUtils.SharedPreferencesCompat.apply(SPUtils.getInstanceEdit(mContext).putString(UserInfoEnum.USER_TOKEN.VALUE, userToken));
        }
        return "";
    }

    /**
     * 刷新token
     *
     * @param userInfoMethod
     * @param refreshToken
     * @return
     */
    public String refreshToken(UserInfoMethod userInfoMethod, String refreshToken) {
        if (userInfoMethod == UserInfoMethod.GET) {
            return SPUtils.getInstance(mContext).getString(UserInfoEnum.REFRESH_TOKEN.VALUE, refreshToken);
        } else {
            SPUtils.SharedPreferencesCompat.apply(SPUtils.getInstanceEdit(mContext).putString(UserInfoEnum.REFRESH_TOKEN.VALUE, refreshToken));
        }
        return "";
    }

    /**
     * token域
     *
     * @param userInfoMethod
     * @param tokenScope
     * @return
     */
    public String tokenScope(UserInfoMethod userInfoMethod, String tokenScope) {
        if (userInfoMethod == UserInfoMethod.GET) {
            return SPUtils.getInstance(mContext).getString(UserInfoEnum.TOKEN_SCOPE.VALUE, tokenScope);
        } else {
            SPUtils.SharedPreferencesCompat.apply(SPUtils.getInstanceEdit(mContext).putString(UserInfoEnum.TOKEN_SCOPE.VALUE, tokenScope));
        }
        return "";
    }

    /**
     * token类型
     *
     * @param userInfoMethod
     * @param tokenType
     * @return
     */
    public String tokenType(UserInfoMethod userInfoMethod, String tokenType) {
        if (userInfoMethod == UserInfoMethod.GET) {
            return SPUtils.getInstance(mContext).getString(UserInfoEnum.TOKEN_TYPE.VALUE, tokenType);
        } else {
            SPUtils.SharedPreferencesCompat.apply(SPUtils.getInstanceEdit(mContext).putString(UserInfoEnum.TOKEN_TYPE.VALUE, tokenType));
        }
        return "";
    }

    /**
     * deviceId
     *
     * @param userInfoMethod
     * @param deviceId
     * @return
     */
    public String deviceId(UserInfoMethod userInfoMethod, String deviceId) {
        if (userInfoMethod == UserInfoMethod.GET) {
            return SPUtils.getInstance(mContext).getString(UserInfoEnum.DEVICE_ID.VALUE, deviceId);
        } else {
            SPUtils.SharedPreferencesCompat.apply(SPUtils.getInstanceEdit(mContext).putString(UserInfoEnum.DEVICE_ID.VALUE, deviceId));
        }
        return "";
    }

    /**
     * 获取登录状态
     *
     * @param userInfoMethod
     * @param value
     * @return
     */
    public boolean loginState(UserInfoMethod userInfoMethod, boolean value) {
        if (userInfoMethod == UserInfoMethod.GET) {
            return SPUtils.getInstance(mContext).getBoolean(UserInfoEnum.LOGIN_STATE.VALUE, value);
        } else {
            SPUtils.SharedPreferencesCompat.apply(SPUtils.getInstanceEdit(mContext).putBoolean(UserInfoEnum.LOGIN_STATE.VALUE, value));
        }
        return false;
    }

    /**
     * 用户id
     *
     * @param userInfoMethod
     * @param value
     * @return
     */
    public String userId(UserInfoMethod userInfoMethod, String value) {
        if (userInfoMethod == UserInfoMethod.GET) {
            return SPUtils.getInstance(mContext).getString(UserInfoEnum.USER_ID.VALUE, value);
        } else {
            SPUtils.SharedPreferencesCompat.apply(SPUtils.getInstanceEdit(mContext).putString(UserInfoEnum.USER_ID.VALUE, value));
        }
        return "";
    }

    /**
     * 用户电话
     *
     * @param userInfoMethod
     * @param value
     * @return
     */
    public String userPhone(UserInfoMethod userInfoMethod, String value) {
        if (userInfoMethod == UserInfoMethod.GET) {
            return SPUtils.getInstance(mContext).getString(UserInfoEnum.PHONE.VALUE, value);
        } else {
            SPUtils.SharedPreferencesCompat.apply(SPUtils.getInstanceEdit(mContext).putString(UserInfoEnum.PHONE.VALUE, value));
        }
        return "";
    }

    /**
     * 用户昵称
     *
     * @param userInfoMethod
     * @param value
     * @return
     */
    public String userNickName(UserInfoMethod userInfoMethod, String value) {
        if (userInfoMethod == UserInfoMethod.GET) {
            return SPUtils.getInstance(mContext).getString(UserInfoEnum.NICK_NAME.VALUE, value);
        } else {
            SPUtils.SharedPreferencesCompat.apply(SPUtils.getInstanceEdit(mContext).putString(UserInfoEnum.NICK_NAME.VALUE, value));
        }
        return "";
    }

    /**
     * 用户头像
     *
     * @param userInfoMethod
     * @param value
     * @return
     */
    public String userAvatar(UserInfoMethod userInfoMethod, String value) {
        if (userInfoMethod == UserInfoMethod.GET) {
            return SPUtils.getInstance(mContext).getString(UserInfoEnum.HEAD_AVATAR.VALUE, value);
        } else {
            SPUtils.SharedPreferencesCompat.apply(SPUtils.getInstanceEdit(mContext).putString(UserInfoEnum.HEAD_AVATAR.VALUE, value));
        }
        return "";
    }

    /**
     * 用户性别
     *
     * @param userInfoMethod
     * @param value
     * @return
     */
    public int userSex(UserInfoMethod userInfoMethod, int value) {
        if (userInfoMethod == UserInfoMethod.GET) {
            return SPUtils.getInstance(mContext).getInt(UserInfoEnum.SEX.VALUE, value);
        } else {
            SPUtils.SharedPreferencesCompat.apply(SPUtils.getInstanceEdit(mContext).putInt(UserInfoEnum.SEX.VALUE, value));
        }
        return -1;
    }

    /**
     * 用户船名
     * @param userInfoMethod
     * @param value
     * @return
     */
    public String userShipName(UserInfoMethod userInfoMethod, String value) {
        if (userInfoMethod == UserInfoMethod.GET) {
            return SPUtils.getInstance(mContext).getString(UserInfoEnum.SHIP_NAME.VALUE, value);
        } else {
            SPUtils.SharedPreferencesCompat.apply(SPUtils.getInstanceEdit(mContext).putString(UserInfoEnum.SHIP_NAME.VALUE, value));
        }
        return "";
    }

    /**
     * 用户账号
     * @param userInfoMethod
     * @param value
     * @return
     */
    public String userAccount(UserInfoMethod userInfoMethod, String value) {
        if (userInfoMethod == UserInfoMethod.GET) {
            return SPUtils.getInstance(mContext).getString(UserInfoEnum.ACCOUNT.VALUE, value);
        } else {
            SPUtils.SharedPreferencesCompat.apply(SPUtils.getInstanceEdit(mContext).putString(UserInfoEnum.ACCOUNT.VALUE, value));
        }
        return "";
    }

    /**
     * 是否第一次安装
     *
     * @param userInfoMethod
     * @param value
     * @return
     */
    public boolean firstInstall(UserInfoMethod userInfoMethod, boolean value) {
        if (userInfoMethod == UserInfoMethod.GET) {
            return SPUtils.getInstance(mContext).getBoolean(UserInfoEnum.FIRST_INSTALL.VALUE, value);
        } else {
            SPUtils.SharedPreferencesCompat.apply(SPUtils.getInstanceEdit(mContext).putBoolean(UserInfoEnum.FIRST_INSTALL.VALUE, value));
        }
        return false;
    }

    /**
     * 是否第一次打开，需要弹出隐私页面
     *
     * @param userInfoMethod
     * @param value
     * @return
     */
    public boolean firstOpen(UserInfoMethod userInfoMethod, boolean value) {
        if (userInfoMethod == UserInfoMethod.GET) {
            return SPUtils.getInstance(mContext).getBoolean(UserInfoEnum.FIRST_OPEN.VALUE, value);
        } else {
            SPUtils.SharedPreferencesCompat.apply(SPUtils.getInstanceEdit(mContext).putBoolean(UserInfoEnum.FIRST_OPEN.VALUE, value));
        }
        return false;
    }

    /**
     * 是否打开通知权限，需要弹出权限授权弹窗
     *
     * @param userInfoMethod
     * @param value
     * @return
     */
    public boolean firstNotificationPermission(UserInfoMethod userInfoMethod, boolean value) {
        if (userInfoMethod == UserInfoMethod.GET) {
            return SPUtils.getInstance(mContext).getBoolean(UserInfoEnum.FIRST_NOTIFICATION_PERMISSION.VALUE, value);
        } else {
            SPUtils.SharedPreferencesCompat.apply(SPUtils.getInstanceEdit(mContext).putBoolean(UserInfoEnum.FIRST_NOTIFICATION_PERMISSION.VALUE, value));
        }
        return false;
    }

    /**
     * 是否打开定位权限，需要弹出权限授权弹窗
     *
     * @param userInfoMethod
     * @param value
     * @return
     */
    public boolean firstLocationPermission(UserInfoMethod userInfoMethod, boolean value) {
        if (userInfoMethod == UserInfoMethod.GET) {
            return SPUtils.getInstance(mContext).getBoolean(UserInfoEnum.FIRST_LOCATION_PERMISSION.VALUE, value);
        } else {
            SPUtils.SharedPreferencesCompat.apply(SPUtils.getInstanceEdit(mContext).putBoolean(UserInfoEnum.FIRST_LOCATION_PERMISSION.VALUE, value));
        }
        return false;
    }

    /**
     * 报港保存信息
     *
     * @param userInfoMethod
     * @param value
     * @return
     */
    public String remoteReportSaveInfo(UserInfoMethod userInfoMethod, String value) {
        if (userInfoMethod == UserInfoMethod.GET) {
            return SPUtils.getInstance(mContext).getString(UserInfoEnum.REMOTE_REPORT_SAVE_INFO.VALUE, value);
        } else {
            SPUtils.SharedPreferencesCompat.apply(SPUtils.getInstanceEdit(mContext).putString(UserInfoEnum.REMOTE_REPORT_SAVE_INFO.VALUE, value));
        }
        return "";
    }

    /**
     * 记住账号
     *
     * @param userInfoMethod
     * @param value
     * @return
     */
    public String rememberAccount(UserInfoMethod userInfoMethod, String value) {
        if (userInfoMethod == UserInfoMethod.GET) {
            return SPUtils.getInstance(mContext).getString(UserInfoEnum.REMEMBER_LOGIN_ACCOUNT.VALUE, value);
        } else {
            SPUtils.SharedPreferencesCompat.apply(SPUtils.getInstanceEdit(mContext).putString(UserInfoEnum.REMEMBER_LOGIN_ACCOUNT.VALUE, value));
        }
        return "";
    }

    /**
     * 记住密码是否修改了默认值
     *
     * @param userInfoMethod
     * @param value
     * @return
     */
    public boolean rememberPasswordChangeDefault(UserInfoMethod userInfoMethod, boolean value) {
        if (userInfoMethod == UserInfoMethod.GET) {
            return SPUtils.getInstance(mContext).getBoolean(UserInfoEnum.REMEMBER_LOGIN_PASSWORD_CHANGE_DEFAULT.VALUE, false);
        } else {
            SPUtils.SharedPreferencesCompat.apply(SPUtils.getInstanceEdit(mContext).putBoolean(UserInfoEnum.REMEMBER_LOGIN_PASSWORD_CHANGE_DEFAULT.VALUE, value));
        }
        return false;
    }

    /**
     * 记住密码
     *
     * @param userInfoMethod
     * @param value
     * @return
     */
    public String rememberPassword(UserInfoMethod userInfoMethod, String value) {
        if (userInfoMethod == UserInfoMethod.GET) {
            return SPUtils.getInstance(mContext).getString(UserInfoEnum.REMEMBER_LOGIN_PASSWORD.VALUE, value);
        } else {
            SPUtils.SharedPreferencesCompat.apply(SPUtils.getInstanceEdit(mContext).putString(UserInfoEnum.REMEMBER_LOGIN_PASSWORD.VALUE, value));
        }
        return "";
    }
}
